package com.eqdd.yiqidian.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.eqdd.yiqidian.base.AppConstent;

import java.io.File;
import java.util.HashMap;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import io.rong.message.ImageMessage;

import static com.jrmf360.rylib.wallet.JrmfWalletClient.getApplicationContext;

/**
 * Created by lzh on 17-1-10.
 */

public class ShareUtil {

    private static final String defaultImg = "https://www.eqid.top:8009/image/EQD20171212151233.png";

    public static String getWorkCircleUrl(String workId, String guid) {
        return "https://www.eqidd.com/Friend/friend.html?workid=" + workId + "&guid=" + guid;
    }

    public static void share2QQ(String url, String fileUrl) {
        ShareParams shareParams = new ShareParams();
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        shareParams.setUrl(url);
        shareParams.setImageUrl(fileUrl);
        JShareInterface.share(QQ.Name, shareParams, new PlatActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                System.out.println("分享成功");
            }

            @Override
            public void onError(Platform platform, int i, int i1, Throwable throwable) {
                System.out.println("分享失败" + throwable.toString());
            }

            @Override
            public void onCancel(Platform platform, int i) {
                System.out.println("分享取消");
            }
        });
    }

    public static void share2QQ(String url) {
        share2QQ(url, defaultImg);
    }

    /**
     * 分享功能
     *
     * @param context       上下文
     * @param activityTitle Activity的名字
     * @param msgTitle      消息标题
     * @param msgText       消息内容
     */
    public static void shareMsgText(Context context, String activityTitle, String msgTitle, String msgText) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain"); // 纯文本
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }

    public static void shareMsgImage(Context context, String activityTitle, String msgTitle,
                                     String imgPath) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        if (imgPath == null || imgPath.equals("")) {
            intent.setType("text/plain"); // 纯文本
        } else {
            File f = new File(imgPath);
            if (f != null && f.exists() && f.isFile()) {
                intent.setType("image/*");
                Uri u = null;
                Uri myUri = Uri.fromFile(f);
                AppConstent.messageContent = ImageMessage.obtain(myUri, myUri);

                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                    u = FileProvider.getUriForFile(getApplicationContext(),
                            "com.eqdd.yiqidian.fileProvider", f);
                } else {
                    u = Uri.fromFile(f);
                }
//                 = Uri.fromFile(f);
                intent.putExtra(Intent.EXTRA_STREAM, u);
            }
        }
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
//        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }

    public static void shareMsgImage(Context context, String activityTitle, String msgTitle,
                                     Uri uri) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
//        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }


    public static void shareMsgVoice(Context context, String activityTitle, String msgTitle,
                                     File f) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        if (f != null && f.exists() && f.isFile()) {
            intent.setType("audio/*");
            Uri u = Uri.fromFile(f);
            intent.putExtra(Intent.EXTRA_STREAM, u);
        }
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
//        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }

    public static void shareMsgVoice(Context context, String activityTitle, String msgTitle,
                                     Uri uri) {
        Intent intent = new Intent(Intent.ACTION_SEND);

        intent.setType("audio/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
//        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }
}
